import {
  get,
  add
} from '../../utils/db'
import {
  multiUpload
} from '../../utils/tools'
const app = getApp();

Page({
  data: {
    content: "", //input框内容
    files: [], //图片
    list: [] //菜谱分类
  },
  //菜单名称
  cdmc(e) {
    this.data.content = e.detail.value;
  },
  //菜谱分类
  async onLoad() {
    var result = await get("class")
    this.setData({
      list: result.data
    })
  },
  //图片
  selectImage(e) {
    var result = e.detail.tempFilePaths;
    var files = result.map(item => {
      return {
        url: item
      }
    })
    this.setData({
      files
    })
  },
  //提交表单
  async fbcd(e) {
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    //1.添加到云存储  转成数组对象形式
    var newArr = this.data.files.map(item => {
      return item.url
    })
    var result = await multiUpload(newArr);
    //转成数组字符串类型
    var image = result.map(item => {
      return item.fileID
    })
    //2.添加到数据库
    var result = await add("menu", {
      name: e.detail.value.name,
      classId: e.detail.value.classId,
      intro: e.detail.value.intro,
      images: image,
      nickName: app.globalData.userInfo.nickName,
      avatarUrl: app.globalData.userInfo.avatarUrl,
      likes: 0,
      status: 1,
      views: 0,
      addtime: new Date().getTime()
    })
    wx.hideLoading();
    //成功弹窗
    wx.showToast({
      title: '添加成功',
      duration: 1500
    })
    //跳转到my
    setTimeout(() => {
      wx.navigateBack()
    }, 1500)
  }
})